﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.AutoMapperExtend;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.ActionResultHelp;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application.YH
{
    /// <summary>
    /// 隐患公共帮助类
    /// </summary>
    [AutoDi()]
    public class YHService
    {
        readonly IDataDicHelp _dataDicHelp;
        readonly SysAreaRepository _sysAreaRepository;
        private static object obj = new object();
        readonly CoreDBContext _coreDBContext;
        public YHService(IDataDicHelp dataDicHelp, SysAreaRepository sysAreaRepository, CoreDBContext coreDBContext)
        {
            _dataDicHelp = dataDicHelp;
            _sysAreaRepository = sysAreaRepository;
            _coreDBContext = coreDBContext;
        }
        /// <summary>
        /// 生成台账编码
        /// </summary>
        /// <param name="Region"></param>
        /// <returns></returns>
        public AjaxJsonModel GetBookCode(Guid? Region)
        {
            lock (obj)
            {
                var areaEntity = _coreDBContext.Set<SysArea>().Where(a => a.Id == Region).FirstOrDefault();
                if (areaEntity == null) return new AjaxJsonModel()
                {
                    result = "false",
                    message = "区域不存在"
                };

                var department = _coreDBContext.Set<SysDepartment>().Where(a => a.Id == areaEntity.SysDepartmentID).FirstOrDefault();
                if (department == null) return new AjaxJsonModel()
                {
                    result = "false",
                    message = "部门不存在"
                };
                if (String.IsNullOrEmpty(department.Code)) return new AjaxJsonModel()
                {
                    result = "false",
                    message = "部门编码不存在"
                };
                //当年当月当前区域共有几个台账
                var bookCount = _coreDBContext.Set<YH_StandingBook>().Where(a => a.RiskRegion == Region && a.CreateTime.Value.Year == DateTime.Now.Year && a.CreateTime.Value.Month == DateTime.Now.Month).Count();
                bookCount += 1;
                //补全
                var strNumber = bookCount.ToString().PadLeft(4, '0');
                //年月
                var nowMonth = DateTime.Now.ToString("yyyyMM").ToString();

                return new AjaxJsonModel()
                {
                    result = "true",
                    data = department.Code + nowMonth + strNumber
                };
            }


        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="RiskLeve"></param>
        /// <returns></returns>
        public Guid GetRiskLevelId(string RiskLeve)
        {
            var riskLevelId = Guid.Empty;
            var list = _dataDicHelp.GeDataDicValueByCategory(YH_GradeService.DATA_DIC_KEY);
            list.ForEach(a =>
            {
                if (a.Value == RiskLeve)
                    riskLevelId = Guid.Parse(a.Key);
            });
            return riskLevelId;
        }

        /// <summary>
        /// 
        /// 根据工厂ID获取区域信息
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public async Task<List<SysArea>> GetAreaByFactoryAsync()
        {
            return await _sysAreaRepository.QueryBase().ToListAsync();
        }

        /// <summary>
        /// 找出区域集合内，某名称的区域信息
        /// </summary>
        /// <param name="sysAreas"></param>
        /// <param name="areas"></param>
        /// <returns></returns>
        public SysArea FindAreaOfAreaListAsync(List<SysArea> sysAreas, string areasName)
        {
            return sysAreas.Where(a => a.Name.Equals(areasName)).FirstOrDefault();
        }
    }
}
